package com.copro.chemdb.subject;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import org.apache.struts.action.ActionForm;
import org.ihelpuoo.lang.BooleanUtils;
import org.ihelpuoo.lang.StringUtils;

import com.copro.chemdb.subject.opt.Option;

public class Subject extends ActionForm {

	public static final int DEFAULT = 0;
	public static final int TEXT = 1;
	public static final int SINGLE_SELECT = 2;
	public static final int MULTI_SELECT = 3;
	public static final int ENUM = 4;

	private static final long serialVersionUID = 5259026011211607280L;

	public Subject() {
		options = new LinkedList<Option>();
	}

	public Subject(ResultSet rs) throws SQLException {
		this();
		id = rs.getInt("id");
		code = rs.getString("code");
		title = rs.getString("title");
		name = rs.getString("name");
		brief = rs.getString("brief");
		type = rs.getInt("type");
		parentId = rs.getInt("id_T_subject");
		quesId = rs.getInt("id_T_questionnaire");
		order = rs.getInt("order");
		pattern = rs.getString("pattern");
		rule = rs.getString("rule");
		enumType = rs.getInt("enumType");
		canQuery = BooleanUtils.parseBoolean(rs.getString("canQuery"));
		indb = BooleanUtils.parseBoolean(rs.getString("indb"));
		inques = BooleanUtils.parseBoolean(rs.getString("inques"));
	}

	private int id;
	private String code;
	private String title;
	private String brief;
	private String name;
	private int type;
	private int parentId;
	private int quesId;
	private int order;
	private String pattern;
	private String rule;
	private int enumType;
	private boolean canQuery;
	private List<Option> options;
	private boolean indb;
	private boolean inques;

	public boolean isInques() {
		return inques;
	}

	public void setInques(boolean inques) {
		this.inques = inques;
	}

	public boolean isIndb() {
		return indb;
	}

	public void setIndb(boolean indb) {
		this.indb = indb;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setBrief(String brief) {
		this.brief = brief;
	}

	public String getBrief() {
		if (!StringUtils.isBlank(brief))
			return brief;
		if (!StringUtils.isBlank(name))
			return name;
		if (null == title)
			return "[NULL]";
		if (title.length() < 10)
			return this.title;
		return title.substring(0, 10).concat("...");
	}

	public boolean isCanQuery() {
		return canQuery;
	}

	public void setCanQuery(boolean canQuery) {
		this.canQuery = canQuery;
	}

	public List<Option> getOptions() {
		return options;
	}

	public void setOptions(List<Option> options) {
		this.options = options;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public int getParentId() {
		return parentId;
	}

	public void setParentId(int parentId) {
		this.parentId = parentId;
	}

	public int getQuesId() {
		return quesId;
	}

	public void setQuesId(int quesId) {
		this.quesId = quesId;
	}

	public int getOrder() {
		return order;
	}

	public void setOrder(int order) {
		this.order = order;
	}

	public String getPattern() {
		return pattern;
	}

	public void setPattern(String pattern) {
		this.pattern = pattern;
	}

	public String getRule() {
		return rule;
	}

	public void setRule(String rule) {
		this.rule = rule;
	}

	public int getEnumType() {
		return enumType;
	}

	public void setEnumType(int enumType) {
		this.enumType = enumType;
	}

	public boolean isDefault() {
		return DEFAULT == this.type;
	}

	public boolean isText() {
		return TEXT == this.type;
	}

	public boolean isSingleSelect() {
		return SINGLE_SELECT == this.type;
	}

	public boolean isMultiSelect() {
		return MULTI_SELECT == this.type;
	}

	public boolean isHasOptions() {
		return SINGLE_SELECT == this.type || MULTI_SELECT == this.type;
	}

	public boolean isCanStat() {
		if (null == this.getRule())
			return false;
		return !"[default]".equalsIgnoreCase(this.getRule());
	}

	public boolean isEnum() {
		return ENUM == this.type;
	}
}
